#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

using namespace std;

#define mode 0666

class namepipe
{
public:
    namepipe(const char *path):_path(path)
    {
        umask(0);
        // 创建有名管道
        int ret = mkfifo(path, mode);
        if (ret == 0)
        {
            cout << "create success" << endl;
        }
        else
        {
            perror("mkfifo create:");
        }
    }

    ~namepipe()
    {
        int n = unlink(_path.c_str());
        if (n == 0)
        {
            cout << "remove fifo file " << _path << " success" << endl;
        }
        else
        {
            cerr << "remove failed, errno: " << errno << ", errstring: " << strerror(errno) << endl;
        }
    }

private:
    string _path;
};
